package com.hex.liyue.usermodule.presenter;

import com.alibaba.fastjson.JSONObject;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.commonmodule.bean.PhoneNumber;
import com.hex.liyue.commonmodule.manager.HttpManager;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.net.ResponseListener;
import com.hex.liyue.commonmodule.utils.PhoneContactUtils;
import com.hex.liyue.usermodule.bean.MachPhoneResultBean;

import java.util.List;

/**
 * Created by yukuo on 2016/5/11.
 * 这是邀请的逻辑层
 */
public class RecommendPresenter {
    private  IRecommendView miRecommendView;

    /***
     * 获取手机的所有的联系人
     */
    public void getPhoneContact() {
        miRecommendView.showLod("加载中...");
        PhoneContactUtils.getPhoneNumberData(LiYueApplication.getInstance(), new PhoneContactUtils.OnLoadSuccessListener() {
            @Override
            public void onSuccess(List<PhoneNumber> list) {
                matchingPhone(list);
            }

            @Override
            public void onFailure() {
                miRecommendView.showT("获取联系人失败");
            }
        });
    }

    /**
     * 匹配通讯录
     *
     * @param list
     */
    public void matchingPhone(List<PhoneNumber> list) {
        JSONObject jsonObject = new JSONObject();
        for (PhoneNumber phoneNumber : list) {
            jsonObject.put(phoneNumber.getNumber(), phoneNumber.getName());
        }
        HttpManager.getInstance().machingPhone(UserManager.getUserId(),jsonObject, new ResponseListener<MachPhoneResultBean>() {
            @Override
            public void onSuccess(MachPhoneResultBean machPhoneResultBean) {
                miRecommendView.closeLod();
                miRecommendView.setAdapter(machPhoneResultBean);
            }

            @Override
            public void onFailure(String responseresult) {
                miRecommendView.closeLod();
                miRecommendView.showT("匹配联系人失败");
            }
        });
    }

    public RecommendPresenter(IRecommendView iRecommendView) {
        miRecommendView = iRecommendView;

    }
}
